//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BlackProcess.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVolTermStructure.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/LocalVolTermStructure.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CBlackProcess::CBlackProcess (Cephei::QL::IQuote^ x0, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeTS, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ blackVolTS) : CGeneralizedBlackScholesProcess(CBlackProcess::typeid)
{
    CQuote^ _Cx0;
    CYieldTermStructure^ _CriskFreeTS;
    CBlackVolTermStructure^ _CblackVolTS;
    try
    {
#ifdef HANDLE
        _phBlackProcess = NULL;
#endif
        _Cx0 = safe_cast<CQuote^> (x0);
        _Cx0->Lock();
        Handle<QuantLib::Quote>& _x0 = static_cast<Handle<QuantLib::Quote>&> (_Cx0->GetHandle ()); 
        _CriskFreeTS = safe_cast<CYieldTermStructure^> (riskFreeTS);
        _CriskFreeTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _riskFreeTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CriskFreeTS->GetHandle ()); 
        _CblackVolTS = safe_cast<CBlackVolTermStructure^> (blackVolTS);
        _CblackVolTS->Lock();
        Handle<QuantLib::BlackVolTermStructure>& _blackVolTS = static_cast<Handle<QuantLib::BlackVolTermStructure>&> (_CblackVolTS->GetHandle ()); 
        _ppBlackProcess = new boost::shared_ptr<QuantLib::BlackProcess> (new QuantLib::BlackProcess ( _x0,  _riskFreeTS,  _blackVolTS ));
        SetGeneralizedBlackScholesProcess (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackProcess));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx0 != nullptr) _Cx0->Unlock();
        if (_CriskFreeTS != nullptr) _CriskFreeTS->Unlock();
        if (_CblackVolTS != nullptr) _CblackVolTS->Unlock();
    }
}
Cephei::QL::Processes::CBlackProcess::CBlackProcess (boost::shared_ptr<QuantLib::BlackProcess>& childNative, Object^ owner) : CGeneralizedBlackScholesProcess(CBlackProcess::typeid)
{
#ifdef HANDLE
	_phBlackProcess = NULL;
#endif
	_ppBlackProcess = &childNative;
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackProcess));
}
Cephei::QL::Processes::CBlackProcess::CBlackProcess (QuantLib::BlackProcess& childNative, Object^ owner) : CGeneralizedBlackScholesProcess(CBlackProcess::typeid)
{
#ifdef HANDLE
	_phBlackProcess = NULL;
#endif
	_ppBlackProcess = new boost::shared_ptr<QuantLib::BlackProcess> (&childNative);
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackProcess));
    _BlackProcessOwner = owner;
    _GeneralizedBlackScholesProcessOwner = owner;
}

Cephei::QL::Processes::CBlackProcess::CBlackProcess (CBlackProcess^ copy) : CGeneralizedBlackScholesProcess(CBlackProcess::typeid)
{
#ifdef HANDLE
	_phBlackProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackProcess = new boost::shared_ptr<QuantLib::BlackProcess> (copy->GetShared());
        _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackProcess));
    }
}
Cephei::QL::Processes::CBlackProcess::CBlackProcess (PLATFORM::Type^ t) : CGeneralizedBlackScholesProcess(CBlackProcess::typeid)
{
#ifdef HANDLE
	_phBlackProcess = NULL;
#endif
	if (!t->IsSubclassOf(CBlackProcess::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CBlackProcess::CBlackProcess (QuantLib::Handle<QuantLib::BlackProcess>& childNative, Object^ owner)  : CGeneralizedBlackScholesProcess(CBlackProcess::typeid)
{
	_phBlackProcess = &childNative;
	_ppBlackProcess = &static_cast<boost::shared_ptr<QuantLib::BlackProcess>>(childNative.currentLink());
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackProcess));
    _BlackProcessOwner = owner;
}
Cephei::QL::Processes::CBlackProcess::CBlackProcess (QuantLib::Handle<QuantLib::BlackProcess> childNative)  : CGeneralizedBlackScholesProcess(CBlackProcess::typeid)
{
	_phBlackProcess = &childNative;
	_ppBlackProcess = &static_cast<boost::shared_ptr<QuantLib::BlackProcess>>(childNative.currentLink());
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CBlackProcess::CBlackProcess (QuantLib::BlackProcess childNative)  : CGeneralizedBlackScholesProcess(CBlackProcess::typeid)
{
#ifdef HANDLE
	_phBlackProcess = NULL;
#endif
	_ppBlackProcess = new boost::shared_ptr<QuantLib::BlackProcess> (new QuantLib::BlackProcess (childNative));
    _ppGeneralizedBlackScholesProcess = new boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> (boost::dynamic_pointer_cast<QuantLib::GeneralizedBlackScholesProcess> (*_ppBlackProcess));
}
#endif

Cephei::QL::Processes::CBlackProcess::~CBlackProcess ()
{
    if (_ppBlackProcess != NULL)
    {
	    delete _ppBlackProcess;
        _ppBlackProcess = NULL;
    }
}
Cephei::QL::Processes::CBlackProcess::!CBlackProcess ()
{
    if (_ppBlackProcess != NULL)
    {
	    delete _ppBlackProcess;
    }
}
QuantLib::BlackProcess& Cephei::QL::Processes::CBlackProcess::GetReference ()
{
    if (_ppBlackProcess == NULL) throw REFNEW NativeNullException ();
	return **_ppBlackProcess;
}
boost::shared_ptr<QuantLib::BlackProcess>& Cephei::QL::Processes::CBlackProcess::GetShared ()
{
    if (_ppBlackProcess == NULL) throw REFNEW NativeNullException ();
	return *_ppBlackProcess;
}
QuantLib::BlackProcess* Cephei::QL::Processes::CBlackProcess::GetPointer ()
{
    if (_ppBlackProcess == NULL) throw REFNEW NativeNullException ();
	return &**_ppBlackProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackProcess>& Cephei::QL::Processes::CBlackProcess::GetHandle ()
{
	if (_phBlackProcess == NULL)
	{
		_phBlackProcess = new Handle<QuantLib::BlackProcess> (*_ppBlackProcess);
	}
	return *_phBlackProcess;
}
#endif
bool Cephei::QL::Processes::CBlackProcess::HasNative () 
{
	return (_ppBlackProcess != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Processes::IBlackProcess^ Cephei::QL::Processes::CBlackProcess_Factory::Create (Cephei::QL::IQuote^ x0, Cephei::QL::Termstructures::IYieldTermStructure^ riskFreeTS, Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVolTermStructure^ blackVolTS)
{
    return REFNEW CBlackProcess ( x0,  riskFreeTS,  blackVolTS);
}
